package com.workmanager.controller;

import com.alibaba.fastjson.JSONObject;
import com.workmanager.bean.ClassBean;
import com.workmanager.bean.TeacherBean;
import com.workmanager.service.PublishNoticeJobService;
import com.workmanager.service.TeacherClassService;
import com.workmanager.util.springUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by liao on 2016/9/5.
 */
@Controller
@RequestMapping("/publishController")
public class PublishController {
    @Autowired
    private PublishNoticeJobService publishNoticeJobService;
    @Autowired
    private TeacherClassService teacherClassService;
    @RequestMapping("/getClassList")
    public void getClassList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TeacherBean tea = (TeacherBean) request.getSession().getAttribute("教师");
        List<ClassBean> list = teacherClassService.getAllClassesByTeacherId(tea.getTeacher_id());
        request.setAttribute("list",list);
        request.getRequestDispatcher("/teacher/publishJob.jsp").forward(request,response);
    }
    @RequestMapping("/publishNotice")
    public void publishNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        String tip = request.getParameter("tip");
        if("notice".equals(tip)){
            String class_id=request.getParameter("class_id");
            String noticeContent = request.getParameter("noticeContent");
            String isTop = request.getParameter("isTop");
            String teacher_id=request.getParameter("teacher_id");
            Map<String,String> map = new HashedMap();
            map.put("class_id",class_id);
            map.put("noticeContent",noticeContent);
            map.put("isTop",isTop);
            map.put("teacher_id",teacher_id);
            boolean result = publishNoticeJobService.publishNotice(map);
            if(result){
                json.put("msg","yes");
            }else{
                json.put("msg","no");
            }
            response.getWriter().print(json.toJSONString());
        }else if("job".equals(tip)){
            Map map = new HashedMap();
            map.put("title",request.getParameter("title"));
            map.put("class_id",request.getParameter("class_id"));
            map.put("teacher_id",request.getParameter("teacher_id"));
            map.put("startTime",request.getParameter("startTime"));
            map.put("endTime",request.getParameter("endTime"));
            map.put("jobDetails",request.getParameter("jobDetails"));
            map.put("jobTask_id","111");
            boolean result = publishNoticeJobService.publishJob(map);
            if(result){
                json.put("msg","yes");
            }else{
                json.put("msg","no");
            }
            response.getWriter().print(json.toJSONString());
        }
    }
}
